//
//  RootViewController.m
//  TestNav4
//
//  Created by wp w on 12-6-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "DLog.h"

#import "TitleViewSegViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.title = @"root view";
    
    arr = [[NSMutableArray alloc] init];
    [arr addObject:@"show/hide bar"];
    [arr addObject:@"left/right btn"];
    [arr addObject:@"titleView - bgImg of bar"];
    [arr addObject:@"titleView - UISegmentedControl"];
    [arr addObject:@"bar's prompt"];
    [arr addObject:@""];
    [arr addObject:@""];
    [arr addObject:@""];
    [arr addObject:@""];
    
//    [arr addObject:@""];
    Log(@"%@", arr);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [arr release];
    arr = nil;
}

-(void)dealloc{
    [arr release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    LogFun();
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LogFun();
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = [arr objectAtIndex:[indexPath row]];
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [arr objectAtIndex:[indexPath row]];
    if([str isEqualToString:@"show/hide bar"]){
        if([self.navigationController.navigationBar isHidden] == YES){
            [self.navigationController.navigationBar setHidden:NO];
        }
        else{
            [self.navigationController.navigationBar setHidden:YES];
            //        [self.navigationController setNavigationBarHidden:YES];
            //        self.navigationController.navigationBar.hidden = YES;
        }
    }
    else if([str isEqualToString:@"left/right btn"]){
        if (self.navigationItem.leftBarButtonItem == nil 
                && self.navigationItem.rightBarButtonItem == nil) {
            UIBarButtonItem *left = [[UIBarButtonItem alloc] 
                                     initWithTitle:@"leftBtn" 
                                     style:UIBarButtonItemStyleBordered 
                                     target:self 
                                     action:@selector(leftBtnPressed)];
            
            self.navigationItem.leftBarButtonItem = left;
            [left release]; 
            
            UIBarButtonItem *right = [[UIBarButtonItem alloc] 
                                      initWithImage:[UIImage imageNamed:@"return.png"] 
                                      style:UIBarButtonItemStylePlain 
                                      target:self 
                                      action:@selector(rightBtnPressed)];
            self.navigationItem.rightBarButtonItem = right;
            [right release];
        }
        else {
            self.navigationItem.leftBarButtonItem = nil;
            self.navigationItem.rightBarButtonItem = nil;
        }
    }
    else if([str isEqualToString:@"titleView - bgImg of bar"]){
        if(self.navigationItem.titleView == nil){
            UIImage *img = [UIImage imageNamed:@"title.png"];
            UIImageView *view = [[UIImageView alloc] initWithImage:img];
            self.navigationItem.titleView = view;
        }
        else {
            self.navigationItem.titleView = nil;
        }
    }
    else if([str isEqualToString:@"titleView - UISegmentedControl"]){
        UIViewController *controller = [[[TitleViewSegViewController alloc]init]autorelease];
        [self.navigationController pushViewController:controller animated:YES];
    }
    else if([str isEqualToString:@"bar's prompt"]){
        if (self.navigationItem.prompt == nil) {
            self.navigationItem.prompt = @"bar's prompt";
        }
        else {
            self.navigationItem.prompt = nil;
        }
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    else if([str isEqualToString:@"bar's prompt"]){
    }
    
}

@end
